<?php

namespace app\store\controller;

use app\store\model\Menus as MenusModel;
use think\request;
use think\Tree;
use think\Db;

/**
 * 菜单管理控制器
 * Class Goods
 * @package app\store\controller
 */
class Menus extends Controller
{
    /**
     * 分类管理
     *
     * @icon fa fa-list
     * @remark 用于统一管理网站的所有分类,分类可进行无限级分类
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->cname='Menus';
        $this->model = model('app\store\model\Menus');
        $tree = Tree::instance();
        $tree->init(collection($this->model->order('id asc')->select()), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'title');
        $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];

        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }

        $this->view->assign("flagList", $this->model->getFlagList());
       // $this->view->assign("typeList", MenusModel::getTypeList());
        $this->view->assign("parentList", $categorydata);
    }

    /**
     * 查看
     */
    public function index()
    {
// 查询数据
       // if ($this->request->isAjax()) {

            $search = $this->request->request("search");
            $type = $this->request->request("type");

            //构造父类select列表选项数据
            $list = [];

            foreach ($this->categorylist as $k => $v) {
                if ($search) {
                    if ($v['type'] == $type && stripos($v['title'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
                            $list = $this->categorylist;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $this->categorylist;
                    } else if ($v['type'] == $type) {
                      $list[] = $v;
                   }
                }
            }
            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

        return $this->fetch('index', compact('list'));
        //return $this->view->fetch();
    }

    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }
	
	
	/*添加菜单*/
	public function add($pid=null){
		if (!$this->request->isAjax()) {
		    if(!empty($pid)){
                $detail=MenusModel::detail($pid);
                $this->assign('detail',$detail);
            }
            $categorylist=$this->categorylist;
            return $this->fetch('add', compact('categorylist','pid'));
        }
        $data=$this->dataChange($this->postData('data'));
        $model = new MenusModel;
        if ($model->add($data)) {
            // $this->renderJson(1,'添加成功');
            return json(1,'添加成功');
        }
        $error = $model->getError() ?: '添加失败';
//        $this->renderJson(0,$error);
        return json(0,$error);
	}

	//编辑菜单
    public function edit($id=null){

        if (!$this->request->isAjax()) {
            if(!empty($id)){
                $detail=MenusModel::detail($id);
                $categorylist=$this->categorylist;
                return $this->fetch('edit', compact('categorylist','detail'));
            }
        }

        $data=$this->dataChange($this->postData('data'));
        //将数组重组
        $model = MenusModel::detail($id);
        // 更新记录
        if ($model->edit($data)) {
            //return $this->renderSuccess('更新成功', url('goods/index'));
           return json(1,__('operation_success'));
        }
        $error = $model->getError() ?: __('operation_failure');
        //return $this->renderError($error);
        return json(0,$error);
    }

    /*删除菜单
    $id:删除的菜单id
    */
    public function delete(){
        if(empty($this->request->param('id'))){
            return json('1',__('illegal_parameters'));
        }
        $model = MenusModel::get($this->request->param('id'));
        if ($model->remove($this->request->param('id'))) {
            echo 1;
        }else{
            echo 0;
        }
    }


}
